package com.cz.fra.android.logic;

import java.util.ArrayList;

import com.cz.fra.android.data.FishReport.CommentTable;
import com.cz.fra.android.entity.Comment;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;

public class CommentLogic {

	private static CommentLogic instance;
	
	private ContentResolver cr;

	private Context mContext;
	
	private ArrayList<Comment> new_comment_list = new ArrayList<Comment>();
	
	private CommentLogic(Context context) {
		mContext = context;
		cr = mContext.getContentResolver();
	}
	
	public static CommentLogic getInstance(Context context) {
		if (instance == null) {
			instance = new CommentLogic(context);
		}
		return instance;
	}
	
	public void insertComment(Comment comment){
		if (cr == null) {
			cr = mContext.getContentResolver();
		}
		Comment temp = isCommentInDB(comment.getCommentId());
		if (temp != null) {
			String where = CommentTable.COMMENT_ID + "=" + "'" + temp.getCommentId() + "'";
			cr.delete(CommentTable.CONTENT_URI, where, null);
		}
		else{
			new_comment_list.add(comment);
		}
		cr.insert(CommentTable.CONTENT_URI, comment.instanceToCV());
	}
	
	public ArrayList<Comment> getComments(int reportId){
		if (cr == null) {
			cr = mContext.getContentResolver();
		}
		ArrayList<Comment> arrayList = new ArrayList<Comment>();
		Cursor cursor = cr.query(CommentTable.CONTENT_URI, null, CommentTable.REPORT_ID+ "=" + "'" + reportId + "'", null, null);
		if (cursor.moveToFirst()) {
			do {
				Comment c = new Comment();
				c.setCommentId(cursor.getInt(cursor.getColumnIndex(CommentTable.COMMENT_ID)));
				c.setCommentBy(cursor.getInt(cursor.getColumnIndex(CommentTable.USER_ID)));
				c.setContent(cursor.getString(cursor.getColumnIndex(CommentTable.COMMENT_CONTENT)));
				c.setReportId(cursor.getInt(cursor.getColumnIndex(CommentTable.REPORT_ID)));
				c.setCommentDate(cursor.getString(cursor.getColumnIndex(CommentTable.COMMENT_DATE)));
				
				arrayList.add(c);
				}while (cursor.moveToNext());
		}
		cursor.close();
		return arrayList;
	}
	
	public Comment isCommentInDB(int commentId){
		Comment comment = null;
		Cursor cursor = cr.query(CommentTable.CONTENT_URI, null, CommentTable.COMMENT_ID+ "=" + "'" + commentId + "'", null, null);
		if (cursor.moveToFirst()) {
			comment = new Comment();
			comment.setCommentId(cursor.getInt(cursor.getColumnIndex(CommentTable.COMMENT_ID)));
		}
		if (cursor != null) {
			cursor.close();
		}
		return comment;
	}

	public ArrayList<Comment> getNewComment(int reportId) {
		return new_comment_list;
	}
	
	
	public void cleanNewCommentList(){
		new_comment_list.clear();
	}

	public int getCommentNum(int reportId) {
		int commentNo = 0;
		Cursor cursor = cr.query(CommentTable.CONTENT_URI, null , CommentTable.REPORT_ID+ "=" + "'" + reportId + "'", null, null);
		if (cursor.moveToFirst()) {
			do {
				commentNo++;
				}while (cursor.moveToNext());
		}
		if (cursor != null) {
			cursor.close();
		}
		return commentNo;
	}
}
